import os
from fuzzywuzzy import process

os.system("title 机电零件管理程序")
f=open("HardWareDatas.csv",'r')
ls=f.readlines()
f.close()
Errorsigns = 0
n = "None"
jumpcode = "None"
SPFSS= "None"
SGNS = "None"
os.system("mode con cols=50 lines=30")
VHTLS = "2022/11/19构建-版本号：0.3" \
        "\n·可用的命令有：" \
        "\n1:op dts----打开上一个精确搜索结果的datasheet，如果不能完全匹配需要用户手动选择" \
        "\n2:<int>---直接输入数字，上一次的结果中的指定行值作为输入(0-99)" \
        "\n3:dts mode---下次的输入作为搜索datasheet的依据" \
        "\n4:cls mode---清理所有已经显示的信息\n5:help---输出本列内容\n6:color mode---设定颜色模式"\
        "\n5:edit mode---打开使用的库文件"
print(VHTLS)
path = "E:\datasheet\hardware"
FileNameList = os.listdir(path)
lpk = FileNameList#str(FileNameList).replace("'","").replace("]","").replace("[","").strip('\n').split(',')
#print(lpk)
PDFknd = ""
count_of_MPS = 0
lpst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,1,2,3,4,5,6,7,8,9,10,11
    ,12,13,14,15,16,17,18,19,20,21,22,23,24,25,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
        24,25,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
#有一个缺点，似乎我需要不断的进行添加这个长度才可以保证在之后不会因为列表过多而崩溃
EPF = 0
while 1:
    #print(ls)
    #print(len(lpk))
    #print(lpst)
    if Errorsigns == 0:
        Errorsigns = 2
        if EPF == 1:
            n = input("\n输入想要查询的数据表：")
            if n.isdigit():
                if len(n) <= 2:
                    print("启用结果指定")
                    #n = input("请选择上一次有效输出的指定行作为输入")
                    n = lpst[int(n)]
        else:
            n = input("\n输入硬件型号/丝印：")
            if n.isdigit():
                if len(n) <= 2:
                    print("启用结果指定")
                    #n = input("请选择上一次有效输出的指定行作为输入")
                    n = lpst[int(n)]
    if Errorsigns == 1:
        n = jumpcode
        Errorsigns = 0
    if n == "cls mode":
        os.system("cls")
        Errorsigns = 0
        '''
        f = open("OrderLines.csv", 'r')
        ls = f.readlines()
        f.close()
        '''
    if n == "help":
        Errorsigns = 0
        print(VHTLS)
    if n == "color mode":
        Errorsigns = 0
        print("正在维护……请等待最新更新发布！")
    if n == "op dts":
        print("您的下一次输入将会是搜索文件的依据")
        EPF = 1
        Errorsigns = 0#int( | EPF == 2)

    if n == "edit mode":
        print("程序会在退出编辑模式后尝试重新装载目录")
        os.system("HardWareDatas.csv")#直接打开的原因是会导致进程会一直到其退出为止，这时候就可以重新装载
        f=open("HardWareDatas.csv",'r')
        ls=f.readlines()
        f.close()
        print("重新装载完毕！")
        Errorsigns = 0
        EPF = 0
    if Errorsigns == 2:
        if EPF == 1 :
            print("正在尝试加载datasheet……")
            PDFknd = n          #默认PDF文档
            print("目标是：",PDFknd)
            #print(lpk[0])
            tks = 1
            i = 0;EPF = 0
            while(tks):
                #print(lpk[i])
                if PDFknd == lpk[i]:
                    os.system("start "+path+"\\"+PDFknd)
                    Errorsigns = 0
                    tks = 0
                    EPF = 0
                if Errorsigns == 2:
                    i += 1
                    if i == len(lpk):
                        tks = 0
                        i = 0
            if Errorsigns == 2:
                print(PDFknd + "--- 无匹配数据表")
                print("·启用模糊搜索方案<试验型>")
                EPF = 1
                Errorsigns = 0
                count_of_MPS = 0
                lpst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 1, 2, 3,
                        4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 1, 2, 3, 4, 5, 6,
                        7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                        10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

                for i in lpk:
                    lt = i.strip('\n').split(',')
                    SPFSS = process.extractOne(n, lt)
                    if int(SPFSS[1] >= 50) & int(len(SPFSS[0]) >= 3):#在这里判断字符串长度是没有意义的
                        CLSET = 0
                        if (SPFSS[1] >= 70):#黄色
                            CLSET = 1
                            if(SPFSS[1] >= 90):#红色
                                CLSET = 2
                        if (CLSET == 0):
                            print(str(count_of_MPS) + ":" + str(SPFSS[0]) + "——" + str(SPFSS[1]) + "%")
                        if (CLSET == 1):
                            print("\033[0;33;40m·" + str(count_of_MPS) + ":" + str(SPFSS[0]) + "——" + str(SPFSS[1]) + "%" + "\033[0m")
                        if (CLSET == 2):
                            print("\033[0;31;40m·" + str(count_of_MPS) + ":" + str(SPFSS[0]) + "——" + str(SPFSS[1]) + "%" + "\033[0m")
                        lpst[count_of_MPS] = str(SPFSS[0])
                        count_of_MPS += 1
                if count_of_MPS == 0:
                    print("源中相似参数全部低于阈值")

    if n == "dts mode":
        EPF = 1
        print("datasheet文件名搜寻模式此次启动")
        Errorsigns = 0


    if EPF == 0:
        for i in ls:
            lt=i.strip('\n').split(',')#split前面是字符串
            #print(lt)
            # _get_best_match(1,n,lt)
            if int(n== lt[0]) | int(n==lt[5]):#| int(n==lt[2])
                Errorsigns = 0
                if format(lt[5]) == "S.P.C.":#丝印码缩写 screen printing code
                    Errorsigns = 1
                    print("\n·丝印对应："+format(lt[6]))
                    jumpcode = format(lt[6])
                if Errorsigns == 0:
                    PDFknd = format(lt[0])
                    lpst = lt
                    print("\n\033[0;32;40m·型号："+format(lt[0])+"\033[0m\n·数量："+format(lt[1])+"\n·封装："+format(lt[2])+"\n\033[0;33;40m·存储单元编码："+format(lt[8])+"\033[0m\n\033[0;34;40m·功能："+format(lt[9])+"\033[0m\n·备注："+format(lt[5])+"\n·价格："+format(lt[3])+"\n·渠道："+format(lt[4])+"\n·唯一码："+format(lt[7]))#lt[0],
        if Errorsigns == 2:
            print(n+"——数据缺失");print("·启用模糊搜索方案<试验型>")
            Errorsigns = 0
            count_of_MPS = 0
            for i in ls:
                lt = i.strip('\n').split(',')
                SPFSS = process.extractOne(n, lt)#要修改为搜索功能弹出芯片型号
                if int(SPFSS[1] >= 50)&int(len(SPFSS[0])>=3):
                    CLSET = 0
                    if (SPFSS[1] >= 70):#黄色
                        CLSET = 1
                        if(SPFSS[1] >= 90):#红色
                            CLSET = 2
                    if (CLSET == 0):
                        print(str(count_of_MPS) + ":" + str(SPFSS[0]) + "——" + str(SPFSS[1]) + "%")
                    if (CLSET == 1):
                        print("\033[0;33;40m·" + str(count_of_MPS) + ":" + str(SPFSS[0]) + "——" + str(SPFSS[1]) + "%" + "\033[0m")
                    if (CLSET == 2):
                        print("\033[0;31;40m·" + str(count_of_MPS) + ":" + str(SPFSS[0]) + "——" + str(SPFSS[1]) + "%" + "\033[0m")
                    lpst[count_of_MPS] = str(SPFSS[0])
                    count_of_MPS += 1
            if count_of_MPS == 0:
                print("源中相似参数全部低于阈值")
